#include "LeafForm.h"
#include <QMouseEvent>
#include <QTimer>
LeafForm::LeafForm(ElementForm* parent)
	:NameForm(parent)
	,_leftDistance(136), _topDistance(142)
{

}

void LeafForm::setElements(const QList<UIElement*> cells)
{
	if (cells.size()==2) {
		setDisplayNum(2);
	}else if(cells.size()==4){
		setDisplayNum(4);
	}
	else if (cells.size() == 3) {
		 setDisplayNum(3);
	}
	ElementForm::setElements(cells);
}


void LeafForm::paintEvent(QPaintEvent* event)
{
	QPainter painter(this);
	painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing, true);

	Node<UIElement>* head = _elements.currentNode();
	Node<UIElement>* tail = _elements.currentNode();
	for (size_t i = 0; i < (_displayNum - 1) / 2; i++) {
		head = head->prev;
		tail = tail->next;
	}
	Node<UIElement>* temp = head;
	while (temp != tail) {
		temp = temp->prev;
		if (temp->data != nullptr) {
			temp->data->setRect(QRect(0, 0, 0, 0));
		}
	}

	for (size_t i = 0; i < _displayNum; i++)
	{
		head = head->next;
		if (head->prev->data != nullptr) {
			QRect rect = QRect(_leftDistance, _topDistance + i * (44 + 30), width() - _leftDistance, 35);
			head->prev->data->drawElement(this, &painter, i==(_displayNum-1)/2, rect);
		}
	}
	NameForm::paintEvent(event);
}

void LeafForm::updateParam()
{
	if (_displayNum == 2) {
		_topDistance = 216;
	}
	else 
	{
		_topDistance = 142;
	}
}


